package Client;

import Message.AccountAddFriendInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.net.Socket;

public class AddFriendActionListener implements ActionListener , MouseListener {
	User user;
	AddFriendUI addFriendUI;

	AddFriendActionListener(AddFriendUI addFriendUI,User user) {
		this.addFriendUI = addFriendUI;
		this.user=user;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() == addFriendUI.bExit) {
			user.setAddFriendUIexist(false);
			addFriendUI.dispose();
		} else if (e.getSource() == addFriendUI.bMin) {
			addFriendUI.setExtendedState(JFrame.ICONIFIED);
		} else if (e.getSource() == addFriendUI.bSearch) {
			String inputString;//获取用户输入的id
			inputString = addFriendUI.inputSearchInfo.getText().trim();
			String messageInfo = "我是" + user.getUserId() + ",想和你加个好友";
			if (!inputString.equals("")) {
				if (this.user.haveThisFriend(inputString)) {
					JOptionPane.showMessageDialog(null, "该用户已在您的好友列表中！", "消息提示", JOptionPane.WARNING_MESSAGE);
				} else {
					AccountAddFriendInfo accountAddFriendInfo = new AccountAddFriendInfo("111111111", inputString, messageInfo);
					try {
						//将accountSignUpinfo传给服务端。
						Socket socket = new Socket(ServerInfo.SERVER_IP_ADDRESS, ServerInfo.SERVER_FRIENDREQUEST_PORT);
						OutputStream outputStream = socket.getOutputStream();
						ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
						objectOutputStream.writeObject(accountAddFriendInfo);
						//获得服务器的回应。
						InputStream inputStream = socket.getInputStream();
						BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
						String res = null;
						res = bufferedReader.readLine();

						if (res.equals("true")) {
							JOptionPane.showMessageDialog(null, "好友添加成功！", "消息提示", JOptionPane.WARNING_MESSAGE);
						} else {
							JOptionPane.showMessageDialog(null, "查找失败，请检查您输入的账号！", "消息提示", JOptionPane.WARNING_MESSAGE);
						}
					} catch (IOException ex) {
						JOptionPane.showMessageDialog(null, "处理用户添加好友的服务器不在线！", "消息提示", JOptionPane.WARNING_MESSAGE);
					}
				}
			}

		}
	}
	@Override
	public void mouseClicked(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		if(e.getSource()==addFriendUI.bExit){
			addFriendUI.bExit.setContentAreaFilled(true);
			addFriendUI.bExit.setBackground(Color.red);
		}
		if(e.getSource()==addFriendUI.bSearch){
			addFriendUI.bSearch.setContentAreaFilled(true);
			addFriendUI.bSearch.setBackground(Color.WHITE);
		}
		if(e.getSource()==addFriendUI.bMin){
			addFriendUI.bMin.setContentAreaFilled(true);
			addFriendUI.bMin.setBackground(Color.white);
		}
	}

	@Override
	public void mouseExited(MouseEvent e) {
		if(e.getSource()==addFriendUI.bExit){
			addFriendUI.bExit.setContentAreaFilled(false);
		}
		if(e.getSource()==addFriendUI.bSearch){
			addFriendUI.bSearch.setBackground(Color.CYAN);
		}
		if(e.getSource()==addFriendUI.bMin){
			addFriendUI.bMin.setContentAreaFilled(false);
		}
	}
}
